package com.cgtong.android.mvp.presenter;

import com.cgtong.android.bean.MonthYearReportBean;
import com.cgtong.android.http.ConstantManager;
import com.cgtong.android.http.callback.BaseCallBack;
import com.cgtong.android.mvp.contract.MonthReportContract;
import com.cgtong.android.mvp.model.MonthReportModel;
import com.zhouyou.http.exception.ApiException;

import java.util.List;

/**
 * Created by chunc on 2017/10/31.
 */

public class MonthReportPresenter implements MonthReportContract.Presenter {

    private MonthReportContract.Model mModle;
    private MonthReportContract.View mView;

    public MonthReportPresenter(MonthReportContract.View view) {
        this.mView = view;
        mModle = new MonthReportModel();
    }


    @Override
    public void httpMonthReportData(String beginMonth, final boolean isRefresh) {
        if (isRefresh) {
            mView.setEnableLoadMore(false);
        }
        mModle.httpMonthReportData(beginMonth, new BaseCallBack<List<MonthYearReportBean>>() {
            @Override
            public void onSuccess(List<MonthYearReportBean> monthYearReportBeen) {
                if (mView != null) {
                    if (isRefresh) {
                        mView.setRefreshing(false);
                        mView.setEnableLoadMore(true);
                        if (monthYearReportBeen == null || monthYearReportBeen.size() == 0) {
                            mView.onNullData();
                            return;
                        } else if (monthYearReportBeen.size() < ConstantManager.PROBLEMS_MAX_MONTH_REPORT_COUNT) {
                            mView.onLoadMoreEnd(isRefresh);
                        }
                        mView.onHttpMonthReportDataSuccess(monthYearReportBeen, isRefresh);
                    } else {
                        mView.onHttpMonthReportDataSuccess(monthYearReportBeen, isRefresh);
                        if (monthYearReportBeen.size() < ConstantManager.PROBLEMS_PAGE_SIZE) {
                            mView.onLoadMoreEnd(isRefresh);
                        }
                    }
                }
            }


            @Override
            public void onError(ApiException e) {
                if (mView != null) {
                    mView.onError(e);
                }
            }
        });
    }
}
